package com.molyfun.parents.activity.store.view;

import android.content.Context;
import android.os.Handler;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.molyfun.parents.R;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by william(liuchao.hit@gmail.com).
 * Date:10/18/16.
 */
public class ExpenseRecordTopBar extends LinearLayout implements View.OnClickListener{

    private int mChooseTab = ExpenseRecordTopChooseTab.BAR_TO_AFFIRM;

    public static interface ExpenseRecordTopChooseTab{
        public static final int BAR_TO_AFFIRM = 1001;
        public static final int BAR_CONSUMED = 1002;
    }

    protected LayoutInflater layoutInflater;

    private Handler handler = new Handler();
    private ExpenseRecordTopBarClickListener listener;
    private Context context;
    private boolean inited;

    @InjectView(R.id.expense_to_affirm)
    TextView mExpenseAffirm;
    @InjectView(R.id.expense_consumed)
    TextView mExpenseConsumed;


    public ExpenseRecordTopBar(Context context, AttributeSet attrs, int defStyle) {
        this(context, attrs);
    }

    public ExpenseRecordTopBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public ExpenseRecordTopBar(Context context) {
        super(context);
        init(context, null);
    }

    private void init(Context context, AttributeSet attrs) {
        this.context = context;
        layoutInflater = LayoutInflater.from(context);
        layoutInflater.inflate(R.layout.widget_expense_record_top_choose_bar, this);
        ButterKnife.inject(this);
        mExpenseAffirm.setOnClickListener(this);
        mExpenseConsumed.setOnClickListener(this);
    }

    public int getCurrentChooseTab(){
        return mChooseTab;
    }

    /**
     * init view
     *
     */
    public void init() {
        if(inited){
            return;
        }

        inited = true;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.expense_to_affirm:
                if(mChooseTab != ExpenseRecordTopChooseTab.BAR_TO_AFFIRM){
                    if(listener != null){
                        listener.onTopBarClick(ExpenseRecordTopChooseTab.BAR_TO_AFFIRM);
                    }
                    setTabClick(ExpenseRecordTopChooseTab.BAR_TO_AFFIRM);
                }
                break;
            case R.id.expense_consumed:
                if(mChooseTab != ExpenseRecordTopChooseTab.BAR_CONSUMED){
                    if(listener != null){
                        listener.onTopBarClick(ExpenseRecordTopChooseTab.BAR_CONSUMED);
                    }
                    setTabClick(ExpenseRecordTopChooseTab.BAR_CONSUMED);
                }
                break;
        }
    }

    /**
     * only view
     * @param tab
     */
    public void setTabClick(int tab){
        if(tab == ExpenseRecordTopChooseTab.BAR_TO_AFFIRM){
            mExpenseAffirm.setBackgroundResource(R.drawable.bg_left_rounded_red);
            mExpenseAffirm.setTextColor(ContextCompat.getColor(context, R.color.color_white));
            mExpenseConsumed.setBackgroundResource(R.drawable.bg_right_rounded_red_hollow);
            mExpenseConsumed.setTextColor(ContextCompat.getColor(context, R.color.color_base_title_bar));
            mChooseTab = ExpenseRecordTopChooseTab.BAR_TO_AFFIRM;
        }else{
            mExpenseAffirm.setBackgroundResource(R.drawable.bg_left_rounded_red_hollow);
            mExpenseAffirm.setTextColor(ContextCompat.getColor(context, R.color.color_base_title_bar));
            mExpenseConsumed.setBackgroundResource(R.drawable.bg_right_rounded_red);
            mExpenseConsumed.setTextColor(ContextCompat.getColor(context, R.color.color_white));
            mChooseTab = ExpenseRecordTopChooseTab.BAR_CONSUMED;
        }
    }

    public void setExpenseRecordTopBarClickListener(ExpenseRecordTopBarClickListener listener) {
        this.listener = listener;
    }

    public static interface ExpenseRecordTopBarClickListener {
        void onTopBarClick(int chooseTab);
    }

}